<?php namespace app\admin\controller;
use system\model\Category as CategoryModel;
use houdunwang\request\Request;
class Category extends Common
{
    //动作
    public function index(CategoryModel $category)
    {

//        $cateData = $category->paginate(v('config.article_num'));
        $cateData = $category->getAllCate();
        return view('',compact('cateData'));
    }
/*
 * 编辑与增加
 */
    public function post(CategoryModel $category)
    {
        $cate_id = Request::get('cate_id',0,'intval');
        //p($cate_id);
        if ( IS_POST ) {
            //判断是否为编辑
            if($cate_id){//编辑
//                获取该编号数据
                $category = CategoryModel::find($cate_id);
            }
//            将数据保存
            $category->save( Request::post() );
            message( '操作成功' , 'index' );
        }
        //处理旧数据
        if($cate_id){
            //编辑功能
//            echo 1;
            $oldData = CategoryModel::find($cate_id);
            //p($oldData->toArray());
            View::with('oldData',$oldData);
            //所属分类数据
            $cateData = $category->getSonCateData($cate_id);
            //p($cateData);
        }else{
            //添加栏目
            //获取栏目所有数据
            $cateData = $category->getAllCate();
            //p($cateData);
        }
        View::with( 'cateData' , $cateData );
        return view();
    }
/*
 *删除
 * CategoryModel systen->model->del
 */
    public function del(CategoryModel $category){
        //接受参数
        $cate_id = Request::get('cate_id');
//       判断结果
        if($category->del($cate_id)){
//            执行
            return $this->setRedirect('index')->success('删除成功');
        }else{
//            报错
            return $this->error($category->getError()[0]);
        }

    }
}
